package com.budwk.app.sqfw.controllers.admin.datamange;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.models.Sqfw_datamange_community;
import com.budwk.app.sqfw.services.datamange.DataMangeCommunityService;
import com.budwk.app.sqfw.services.datamange.DataMangeDistrictService;
import com.budwk.app.sqfw.vo.SqfwDataMangeCheckVO;
import com.budwk.app.sqfw.vo.SqfwDataMangeVO;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.adminUtils.CommonCheckUtils;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.List;

/**
 * @program: community-admin
 * @description: 每级主表实时数据管理
 * @author: LJT
 * @create: 2024-05-31 16:45
 **/
@IocBean
@At("/platform/sqfw/online/")
@ApiDefinition(tag = "每级主表实时数据管理（后台管理系统）")
@Slf4j
public class SqfwDataMangeController {

    @Inject
    private RedisService redisService;

    @Inject
    private DataMangeCommunityService sqfwFormsummaryOnlineService;

    @Inject
    private DataMangeDistrictService dataMangeDistrictService;

    @Inject
    private DataMangeCommunityService dataMangeCommunityService;

    @Inject
    private SysUserService sysUserService;


    /**
     * 列表查询
     */
    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "列表查询")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "create_date", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = SqfwDataMangeVO.class
    )
    @ApiResponses(
            implementation = SqfwDataMangeVO.class
    )
    @SLog(tag = "公共列表查询（后台管理系统）", value = "${args[0]}}")
    @SaCheckLogin
    public Result<?> list(@Param("..") SqfwDataMangeVO bean) {
        return Result.data(sqfwFormsummaryOnlineService.page(bean));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加数据")
    @ApiResponses
    @SLog(tag = "添加数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> save(@Param("..") SqfwDataMangeVO bean) {
        dataMangeCommunityService.cleanDataByType(com.google.common.collect.Lists.newArrayList(bean.getId()), bean.getType());
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "抽取大数据")
    @ApiResponses
    @SLog(tag = "抽取大数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> parseDataBig(@Param("..") SqfwDataMangeVO bean) {
        sqfwFormsummaryOnlineService.parseDataBig(bean);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "批量清洗数据")
    @ApiResponses
    @SLog(tag = "批量清洗数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> batchCleanData(@Param("..") SqfwDataMangeVO bean) {
        sqfwFormsummaryOnlineService.batchCleanData(bean);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "删除数据")
    @ApiResponses
    @SLog(tag = "删除数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> deleteDataCommunity(@Param("ids") List<String> ids) {
        sqfwFormsummaryOnlineService.deleteData(ids);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "删除数据")
    @ApiResponses
    @SLog(tag = "删除数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> deleteDistrict() {
        dataMangeDistrictService.clear();
        return Result.success();
    }

    @At
    @Ok("json")
    @DELETE
    @ApiOperation(name = "删除数据sss")
    @ApiResponses
    @SLog(tag = "删除数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> deleteCommunity(@Param("..") SqfwDataMangeVO bean) {
        sqfwFormsummaryOnlineService.clear(Cnd.where(Sqfw_datamange_community::getType, "=", bean.getType()));
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "更新数据")
    @ApiResponses
    @SLog(tag = "更新数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> updateData(@Param("..") SqfwDataMangeVO bean) {
        sqfwFormsummaryOnlineService.updateData(bean);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "列表查询")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "create_date", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = SqfwDataMangeVO.class
    )
    @ApiResponses(
            implementation = SqfwDataMangeVO.class
    )
    @SLog(tag = "公共列表查询（后台管理系统）", value = "${args[0]}}")
    @SaCheckLogin
    public Result<?> pageView(@Param("..") SqfwDataMangeVO bean) {
        // 生成缓存键
        Pagination pagination = new Pagination();
        String cacheKey = CommonCheckUtils.generateCacheKey(bean);
        String cachedData = redisService.get(cacheKey);
        if (StringUtils.isNotEmpty(bean.getDelCache()) && "1".equals(bean.getDelCache())) {
            redisService.del(cachedData);
            cachedData = "";
        }
        if (StringUtils.isNotEmpty(cachedData)) {
            pagination = JSONUtil.toBean(cachedData, Pagination.class);
            return Result.data(pagination);
        }
        pagination = sqfwFormsummaryOnlineService.pageViewByType(bean);
        if (ObjectUtils.isEmpty(pagination)) {
            return Result.data(pagination);
        }
        redisService.setex(cacheKey, 60 * 5, JSONUtil.toJsonStr(pagination));
        return Result.data(pagination);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询登录用户最高级别行政区域id", description = "查询登录用户最高级别行政区域id")
    @ApiImplicitParams
    @ApiResponses
    public Result<?> getUserTopAreaLevelId() {
        return Result.data(sqfwFormsummaryOnlineService.getUserTopAreaLevelId());
    }
}
